package com.jwong.test;

import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.dsl.ExecListener;
import io.fabric8.kubernetes.client.dsl.Listenable;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class MysqlPodExec {

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        try (DefaultKubernetesClient kubernetesClient = new DefaultKubernetesClient();) {
            kubernetesClient.pods()
                    .inNamespace("default")
                    .withName("mysql")
                    .readingInput(System.in)
                    .writingOutput(System.out)
                    .writingError(System.err)
                    .withTTY()
                    .usingListener(new SampleListener(countDownLatch))
                    .exec("sh", "-c", "mysql -uroot -pabc123456 -e 'show databases'");
        }
        countDownLatch.await();
    }

    private static class SampleListener implements ExecListener {

        private final CountDownLatch countDownLatch;

        public SampleListener(CountDownLatch countDownLatch) {
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void onClose(int code, String reason) {
            countDownLatch.countDown();
        }
    }


}
